<template>
  <el-form ref="formrefs" label-width="80px" :model="formData" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" />
    </el-form-item>
    <el-form-item label="入职时间">
      <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期" style="width: 100%;" />

    </el-form-item>
    <el-form-item label="活动区域">
      <el-select v-model="formData.region" placeholder="请选择活动区域" style="width:100%">
        <el-option v-for=" item in region" :key="item.id" :label="item.label" :value="item.label" />
      <!-- <el-option label="区域二" value="beijing" /> -->
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formData.departmentName" @focus="getmn" />
      <div v-show="isload" class="tree-box">
        <el-tree :data="treelist" :props="{ label: 'name' }" @node-click="handleNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker v-model="formData.correctionTime" type="date" placeholder="选择日期" style="width: 100%;" />

    </el-form-item>
    <el-form-item>
      <el-button>取消</el-button>
      <el-button type="primary" @click="add">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { type_lx } from './employees.js'
import { addinfo } from '@/api/employees'
import { getmenus } from '@/api/departments.js'

export default {
  data() {
    return {
      isload: false,
      region: Object.keys(type_lx).map(item => { return { id: item, label: type_lx[item] } }),
      formData: {
        username: 'yxyx', // 用户名
        mobile: '13838389438', // 手机号
        formOfEmployment: '1', // 聘用形式
        workNumber: '5564', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间,
        region: ''

      },
      treelist: [],
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    add() {
      this.$refs.formrefs.validate(valid => {
        if (valid) {
          console.log('1')
          this.addinfos()
          this.$emit('success')
        }
      })
    },
    async addinfos() {
      const { data: res } = await addinfo(this.formData)
      console.log(res)
    },
    handleNodeClick(data) {
      if (data.children.length) {
        return
      } else {
        this.formData.departmentName = data.name
        this.isload = false
      }
    },
    getmn() {
      this.isload = true
      this.getmenuss()
    },

    async getmenuss() {
      const { data: res } = await getmenus()
      console.log(res.depts)
      res.depts.shift()
      const list = res.depts
      const arr = list.filter(item => {
        item.children = list.filter(ele => { return item.id === ele.pid })
        return !item.pid
      })
      console.log(arr)
      this.treelist = arr
    }
  }
}
</script>

<style>

</style>
